package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseDistributionPlanDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.FileTechnical;
import com.guodu.entity.PurchaseDistributionPlan;
import com.guodu.entity.PurchasedistributionStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseDistributionPlanService;
import com.guodu.vo.PurchaseDistributionPlanQueryVo;
import com.guodu.vo.PurchaseDistributionPlanVo;

@Service("purchaseDistributionPlanService")
public class PurchaseDistributionPlanServiceImpl implements PurchaseDistributionPlanService {

	
	@Resource
	private PurchaseDistributionPlanDao purchaseDistributionPlanDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	
	@Override
	public long findSizeByVo(PurchaseDistributionPlanQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlanDao.findSizeByVo(pdpq, units);
	}

	@Override
	public List<PurchaseDistributionPlanVo> findByVo(
			PurchaseDistributionPlanQueryVo pdpq, Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseDistributionPlan> list = purchaseDistributionPlanDao.findByVo(pdpq, units, curPage, pageSize);
		List<PurchaseDistributionPlanVo> purchaseDistributionPlanVos = new ArrayList<PurchaseDistributionPlanVo>();
		for(PurchaseDistributionPlan purchaseDistributionPlan : list ){
			PurchaseDistributionPlanVo purchaseDistributionPlanVo = new PurchaseDistributionPlanVo(purchaseDistributionPlan);
			UserInfo userInfo = userInfoDao.findById(purchaseDistributionPlan.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			List<FileTechnical> fileTechnical = purchaseDistributionPlanDao.findFile(purchaseDistributionPlan.getTechnical());
			purchaseDistributionPlanVo.setUnitsName(unitsInfo.getUnitsName());
			purchaseDistributionPlanVo.setIndustry(unitsInfo.getIndustry());
			for (FileTechnical fileTechnical2 : fileTechnical) {
				purchaseDistributionPlanVo.setFileTechnicalName(fileTechnical2.getFileTechnicalName());
				purchaseDistributionPlanVo.setFileName(fileTechnical2.getFileName());
				purchaseDistributionPlanVo.setFileUrl(fileTechnical2.getFileRealname());
			}
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchaseDistributionPlanVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchaseDistributionPlanVo.setUserName(userInfo.getUserName());
			purchaseDistributionPlanVos.add(purchaseDistributionPlanVo);
		}
		return purchaseDistributionPlanVos;
	}

	@Override
	public void save(PurchaseDistributionPlan purchaseDistributionPlan) {
		// TODO Auto-generated method stub
		purchaseDistributionPlanDao.save(purchaseDistributionPlan);
		
	}

	@Override
	public PurchaseDistributionPlan findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlanDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseDistributionPlan purchaseDistributionPlan,Long status) {
		// TODO Auto-generated method stub
		purchaseDistributionPlanDao.update(purchaseDistributionPlan);
		List<PurchasedistributionStatus> entities = new ArrayList<PurchasedistributionStatus>();
		PurchasedistributionStatus entity = new PurchasedistributionStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseDistributionPlan.getCreatePeople());
		entity.setPurchaseId(purchaseDistributionPlan.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseDistributionPlanDao.changeStatus(entities);
	}

	@Override
	public List<PurchasedistributionStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlanDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasedistributionStatus> entities = new ArrayList<PurchasedistributionStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasedistributionStatus entity = new PurchasedistributionStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseDistributionPlanDao.changeStatus(entities);
		purchaseDistributionPlanDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		for (int i = 0; i < delIds.length; i++) {
			PurchaseDistributionPlan purchaseDistributionPlan = purchaseDistributionPlanDao.findById(delIds[i]);
			purchaseDistributionPlanDao.delZi(purchaseDistributionPlan.getTechnical());
		}
		purchaseDistributionPlanDao.delByIds(delIds);
	}

	@Override
	public void saveFile(FileTechnical fileTechnical) {
		// TODO Auto-generated method stub
		purchaseDistributionPlanDao.saveFile(fileTechnical);
	}

	@Override
	public List<FileTechnical> findFile(String fileTechnical) {
		return purchaseDistributionPlanDao.findFile(fileTechnical);
	}

	@Override
	public void delByOne(String technical) {
		// TODO Auto-generated method stub
		purchaseDistributionPlanDao.delZi(technical);
	}

	@Override
	public List<PurchaseDistributionPlan> findAllByVo(
			PurchaseDistributionPlanQueryVo pdpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlanDao.findAllByVo(pdpq,attribute);
	}

	
	public PurchaseDistributionPlanVo findAllMoney(
			PurchaseDistributionPlanQueryVo pdpq, Units units) {
		
		return purchaseDistributionPlanDao.findAllMoney(pdpq,units);
	}

	@Override
	public void examinAll(PurchaseDistributionPlanQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		purchaseDistributionPlanDao.examinAll(pdpq, units);
	}

	
	

	
	
}
